//
//  WXSMainDetailViewController.m
//  SleepBooks
//
//  Created by Ivan_Yang on 2016/9/23.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "WXSMainDetailViewController.h"
#import "WXSDayDetailViewController.h"
#import "WXSWeekDetailViewController.h"
#import "WXSMonthViewController.h"

@interface WXSMainDetailViewController ()

@property (nonatomic, strong) UISegmentedControl * segmentedControl;
@property (nonatomic, strong) WXSDayDetailViewController * dayController;
@property (nonatomic, strong) WXSWeekDetailViewController * weekController;
@property (nonatomic, strong) WXSMonthViewController * monthController;
@property (nonatomic, strong) UIViewController * currentController;//获取当前控制器

- (void)_initializeAppearance;//初始化

@end

@implementation WXSMainDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"日报告";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self _initializeAppearance];
}

#pragma mark - private method

- (void)_initializeAppearance
{
    self.navigationItem.titleView = self.segmentedControl;
    self.currentController = self.dayController;
}

#pragma mark - action

- (void)segmentedControlSelected:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0)
    {
        self.currentController = self.dayController;
        self.title = @"日报告";
    }
    else if (segment.selectedSegmentIndex == 1)
    {
        self.currentController = self.weekController;
        self.title = @"周报告";
    }
    else
    {
        self.currentController = self.monthController;
        self.title = @"月报告";
    }
    
}

- (void)setCurrentController:(UIViewController *)currentController
{
    [self.currentController removeFromParentViewController];
    [self.currentController.view removeFromSuperview];
    _currentController = currentController;
    [self addChildViewController:_currentController];
    [self.view addSubview:_currentController.view];
}

#pragma mark - lazy load

- (UISegmentedControl *)segmentedControl
{
    if (_segmentedControl == nil)
    {
        _segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"日", @"周", @"月"]];
        [_segmentedControl addTarget:self action:@selector(segmentedControlSelected:) forControlEvents:UIControlEventValueChanged];
        _segmentedControl.frame = CGRectMake(0, 0, 150, 30);
        _segmentedControl.selectedSegmentIndex = 0;
    }
    return _segmentedControl;
}

- (WXSDayDetailViewController *)dayController
{
    if (_dayController == nil)
    {
        _dayController = [[WXSDayDetailViewController alloc]init];
        _dayController.view.frame = CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64 - 49);
    }
    return _dayController;
}

- (WXSWeekDetailViewController *)weekController
{
    if (_weekController == nil)
    {
        _weekController = [[WXSWeekDetailViewController alloc]init];
        _weekController.view.frame = CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64 - 49);
    }
    return _weekController;
}

- (WXSMonthViewController *)monthController
{
    if (_monthController == nil)
    {
        _monthController = [[WXSMonthViewController alloc]init];
        _monthController.view.frame = CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64 - 49);
    }
    return _monthController;
}

@end
